﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLib;
using System.Threading;

namespace sample02
{
    public delegate void RenderImage(double kToon);

    public partial class Form1 : Form
    {
        static int ImageWidth = 800;
        static int ImageHeight = 600;
        private CommonLib.Tracer tracer;
        private bool rendering;
        private double kTone = 0.20;

        public Form1()
        {
            InitializeComponent();
            initializeTracer();
        }

        private void initializeTracer()
        {
            CommonLib.Screen screen = new CommonLib.Screen(
                ImageWidth, ImageHeight);
            CommonLib.Scene scene = new CommonLib.Scene();
            tracer = new CommonLib.Tracer(screen, scene);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.pictureBox1.Image = tracer.Screen.Buffer;
            this.ClientSize = pictureBox1.Size;
            this.textBox1.Text = this.kTone.ToString();
            this.timer1.Interval = 100;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e) {
            startRendering();
        }
        private void startRendering() {
            if (rendering) {
                System.Windows.Forms.MessageBox.Show("Rendering has already started");
                return;
            }
            this.rendering = true;
            this.timer1.Enabled = true;

            tracer.OnCompleted += this.renderingFinished;
            RenderImage asyncCall = new RenderImage(tracer.render);
            asyncCall.BeginInvoke(this.kTone, null, null);

        }
        private void renderingFinished(CommonLib.Tracer tcr) {
            pictureBox1.Image = tracer.Screen.Buffer;
            //pictureBox1.Refresh();

            this.rendering = false;
            this.timer1.Enabled = false;
        }

        private void label1_Click(object sender, EventArgs e) {

        }

        private void toneParameterChanged(object sender, EventArgs e) {
            TextBox paramBox = sender as TextBox;
            if (paramBox == null)
                return;

             try {
                this.kTone = double.Parse(paramBox.Text);
            } catch (FormatException exp) {
            }
        }

        private void timer1_Tick(object sender, EventArgs e) {
            int count = this.tracer.NumRenderedPixels;
            int percentage =  (int) System.Math.Floor(100.0 * count / this.tracer.Screen.Size);
            this.progressBar1.Value = percentage;

            TimeSpan elapsed = this.tracer.Swatch.Elapsed;
            //System.Windows.Forms.MessageBox.Show(elapsed.ToString());
            this.textBox2.Text = elapsed.ToString();
        }

        private void label1_Click_1(object sender, EventArgs e) {

        }
    }
}
